package com.situying.hibernate.test;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class QuickStart {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		SessionFactory sessionFactory = new Configuration()
        .configure()// configures settings from hibernate.cfg.xml
        .buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
//		List result = session.createQuery( "from Client c where c.clientName='Jerry Dong'" ).list();
//		for ( Client client : (List<Client>) result ) {
//		    System.out.println( "Client (" +client.getClientId()+":"+ client.getClientName() + " : " + client.getLocation().getLocation()+")" );
//		    for(BookOrder order : client.getBookOrders())
//		    {
//		    	System.out.println("Book ("+order.getSerialNumber()+":"+order.getBookId()+":"+order.getClientId()+")");
//		    	session.delete(order);
//		    }
//		    session.save(client);
//		}
		session.delete((BookOrder)(session.createQuery("from BookOrder bo where bo.serialNumber=6").uniqueResult()));
// 		Client c1 = new Client();
//		c1.setClientName("Jerry Dong");
//		Location l1 = (Location)session.createQuery("from Location l where l.location='West Lake District'").uniqueResult();
//		c1.setLocation(l1);
//		Book book =(Book) session.createQuery("from Book").iterate().next();
//		BookOrder order = new BookOrder();
//		order.setBookId(book.getBookId());
//		order.setClientId(c1.getClientId());
//		order.setBookDate(new Date());
//		Set<BookOrder> orders = new HashSet<BookOrder>();
//		orders.add(order);
//		c1.setBookOrders(orders);
//		List<Book> books = new ArrayList<Book>();
//		Book book1 = new Book();
//		book1.setBookId(1);
//		book1.setBookName("Test Book 1");
//		book1.setAuthor("Test1");
//		books.add(book1);
//		
//		Book book2 = new Book();
//		book2.setBookId(2);
//		book2.setBookName("Test Book 2");
//		book2.setAuthor("Test2");
//		books.add(book2);
//		
//		c1.setBooks(books);
//		session.save(c1);
		session.getTransaction().commit();
		session.close();
	}

}
